package com.seckill.access;

import com.seckill.domain.MiaoshaUser;

/**
 * 用户登陆信息
 * 接口拦截->获取token->获取MiaoshaUser
 * ThreadLocal保证每个请求包含自己的用户
 * @author yjw
 * @date 2022/3/20 1:43
 */
public class UserContext {

    private final static ThreadLocal<MiaoshaUser> USER_HOLDER = new ThreadLocal<>();

    public static void setUserHolder(MiaoshaUser miaoshaUser) {
        USER_HOLDER.set(miaoshaUser);
    }

    public static MiaoshaUser getMiaoshaUser() {
        return USER_HOLDER.get();
    }

    /**
     * 删除ThreadLocal中的数据。防止value产生内存泄漏
     */
    public static void removeUser() {
        USER_HOLDER.remove();
    }


}
